### Male

male <- svyby(~P13_I4_e, ~M1, srs_design, svymean, na = TRUE)

male <- as.data.table(male)

male[, variable := ifelse(M1 == "Mezczyzna", "Male", "Female")]

male <- male[, .(variable, ind = M1, P13_I4_e, se)]

### Young person (< 24 yo)

young <- svyby(~P13_I4_e, ~young, srs_design, svymean, na = TRUE)

young <- as.data.table(young)

young[, variable := "Young"]

young <- young[, .(variable, ind = young, P13_I4_e, se)]

### High economic literacy

highknowl <- svyby(~P13_I4_e, ~highknowledge, srs_design, svymean, na = TRUE)

highknowl <- as.data.table(highknowl)

highknowl[, variable := "High economic literacy"]

highknowl <- highknowl[, .(variable, ind = highknowledge, P13_I4_e, se)]

### City

city <- svyby(~P13_I4_e, ~city, srs_design, svymean, na = TRUE)

city <- as.data.table(city)

city[, variable := "City"]

city <- city[, .(variable, ind = city, P13_I4_e, se)]

### Higher education

highedu <- svyby(~P13_I4_e, ~higheredu, srs_design, svymean, na = TRUE)

highedu <- as.data.table(highedu)

highedu[, variable := "Higher education"]

highedu <- highedu[, .(variable, ind = higheredu, P13_I4_e, se)]

### High income

highinc <- svyby(~P13_I4_e, ~highincome, srs_design, svymean, na = TRUE)

highinc <- as.data.table(highinc)

highinc[, variable := "High income"]

highinc <- highinc[, .(variable, ind = highincome, P13_I4_e, se)]

### Pariticiption in elections

partel <- svyby(~P13_I4_e, ~M17, srs_design, svymean, na = TRUE)

partel <- as.data.table(partel)

partel[, variable := "Participation in elections"]

partel <- partel[, .(variable, ind = M17, P13_I4_e, se)]

### Support for government

supgov <- svyby(~P13_I4_e, ~M_20, srs_design, svymean, na = TRUE)

supgov <- as.data.table(supgov)

supgov[, variable := "Support for government"]

supgov <- supgov[, .(variable, ind = M_20, P13_I4_e, se)]

### Unemployed

unemp <- svyby(~P13_I4_e, ~M11_3_b, srs_design, svymean, na = TRUE) 

unemp <- as.data.table(unemp)

unemp[, variable := "Unemployed"]

unemp <- unemp[, .(variable, ind = M11_3_b, P13_I4_e, se)]

### Self-employed

selfemp <- svyby(~P13_I4_e, ~M12_2_b, srs_design, svymean, na = TRUE) 

selfemp <- as.data.table(selfemp)

selfemp[, variable := "Self-employed"]

selfemp <- selfemp[, .(variable, ind = M12_2_b, P13_I4_e, se)]

### Employed

emp <- svyby(~P13_I4_e, ~M11_1_b, srs_design, svymean, na = TRUE) 

emp <- as.data.table(emp)

emp[, variable := "Employed"]

emp <- emp[, .(variable, ind = M11_1_b, P13_I4_e, se)]

### Student

student <- svyby(~P13_I4_e, ~M11_5_b, srs_design, svymean, na = TRUE) 

student <- as.data.table(student)

student[, variable := "Student"]

student <- student[, .(variable, ind = M11_5_b, P13_I4_e, se)]

### Bind all 

P13_I4_e_cov <- rbind(male,young, highknowl, city, highedu, highinc, partel, supgov, unemp, selfemp, emp, student)

P13_I4_e_cov <- as.data.table(P13_I4_e_cov)

P13_I4_e_cov[, ind := ifelse(ind == "1", "Yes", "No")]

P13_I4_e_cov <- P13_I4_e_cov[, .(variable, ind, P13_I4_e = -P13_I4_e, se)]

P13_I4_e_cov_p <- ggplot(P13_I4_e_cov, aes(x = P13_I4_e, y = variable, color = factor(ind))) +
  geom_point(position = position_dodge(width = 0.5), size = 2) +
  geom_errorbarh(aes(xmin = P13_I4_e - se, xmax = P13_I4_e + se), position = position_dodge(width = 0.5), height = 0.1) +
  scale_color_manual(values = c("darkgreen", "darkred")) + 
  theme_light() + scale_fill_brewer(palette = "Spectral") +
  theme(axis.text.x = element_text(color = "grey20", size = 14, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 14, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 14, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 14, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 14, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 14, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Individual perception error (percentage points)", y = "", color='Indicator')
+ xlim(0, 10)

ggsave("P13_I4_e_cov_p.jpeg", device='jpeg', dpi=1000)
